<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
//分页功能
use think\Paginator;
use think\Paginator\driver\Bootstrap;

class Classdate extends Controller
{
    //展示页面
    public  function index()
    {   
         
       $data = Db::field('c.id,c.coid,c.starttime,c.classname,c.longtime,co.coursename ')
                 ->name('classdate')
                 ->alias('c')
                 ->join('course co','c.coid = co.id')
                 ->paginate(8);
        // var_dump($data);die;
        
          $a = [];
        foreach ($data as $v) {
            $b=date('Y-m-d',$v['starttime']);
            $c = array_push($v,$b);
            $d = $v['coursename'];
            $e = array_push($v,$d);
            $a[] = $v;
        }

        // var_dump($a);die;
         $t=Db::field('coursename')
             ->name('course')
             ->select();
        // var_dump($t);die;
        $e=[];
       foreach ($t as $key => $j) {
         foreach ($j as $k => $s) {
           $e[] = $s;
         }
       }
       
        // var_dump($e);
        // var_dump($a);die;

        return View('',[
            'data'=>$a,
            'e' =>$e
             ]);
    }
 
  

    //执行搜索功能,执行分页功能
    public function  select(Request $request)
    {
       //接收数据
       $p = $request->post();
       $data = [
        'coursename' => $p['coursename'],
        'classname' => $p['classname'],
       ];

        //判断让用户先选课程
        if($data['coursename']==='option1'){
          return $this->error('请先选择所属的课程');
        }
        
        
      $res = Db::field('coursename,id')
               ->name('course')
               ->where('coursename',$data['coursename'])
               ->find();

       //判断是否选择了班期
        if($data['classname']===''){
                // var_dump($res);die;
      $b =Db::field('classname,coid,starttime,longtime,id')
            ->name('classdate')
            ->where('coid',$res['id'])
            ->select();
     

    }else{
          $b =Db::field('classname,coid,starttime,longtime,id')
            ->name('classdate')
            ->where('coid',$res['id'])
            ->where('classname',$data['classname'])
            ->select();

      }

      if(empty($b)){
         return $this->error('你填写的班期不存在,请重新输入');
         }

          $a = [];
        foreach ($b as $v) {
            $c=date('Y-m-d',$v['starttime']);
            $d = array_push($v,$c);
            $g= $res['coursename'];
            $f = array_push($v,$g);
            $a[] = $v;
        }


       $t=Db::field('coursename')
             ->name('course')
             ->select();
        // var_dump($t);die;
        $e=[];
       foreach ($t as $key => $j) {
         foreach ($j as $k => $s) {
           $e[] = $s;
         }
       }
       
        // var_dump($e);
        // var_dump($a);die;
      return view('classdate/index',[
            'data'=>$a,
            'e' =>$e
             ]);

    }



    //执行新增
    public function add()
    {    
          $t=Db::field('coursename')
             ->name('course')
             ->select();
        $e=[];
       foreach ($t as $key => $j) {
         foreach ($j as $k => $s) {
           $e[] = $s;
         }
       }
       
        return View('',[
            'e' =>$e
             ]);
    }
    //提交新增数据
     public function save(Request $request)
    {
    
      $p = $request->post();
      // var_dump($p);die;
        $a=[ 'coursename' =>$p['coursename']];

       $data = [
        'classname' => $p['classname'],
        
        'longtime' => $p['longtime']
       ];
       // var_dump($data);die;
       $res = Db::name('course')
                 ->where('coursename',$a['coursename'])
                 ->find();
        $data['coid']=$res['id'];
        $data['starttime']=time();

            // var_dump($data);die;
            
    //执行新增前的判断
      //1.判断输入的没一条数据都不能为空
          if($data['classname'] ==='' || $data['longtime'] === ''){
           return $this->error('添加的数据不能为空');
          }
     //2.判断新增的班期以前是否存在
        $a = Db::field('classname')
               ->name('classdate')
               ->select();
          $b=[];
        foreach ($a as $k => $v) {
          $b[]=$v['classname'];
        }      
        // var_dump($b);die;
         if(in_array($data['classname'],$b)){
          return  $this->error('您添加的班期已经存在,请重新添加');
        }
        
        if($res > 0){
       $result = Db::name('classdate')
               ->data($data)
               ->insert();
             }
     //执行添加
    if($res > 0){
        return $this ->success('添加成功',url('admin/classdate/index'));
      }else{
        return $this->error('添加失败,请重试');
      }

    }

     //编辑课程(edit)
    public function edit($id)
    {
        //查数据
        $data = Db::name('classdate')
                  ->where('id ='.$id)
                  ->find();
        
      $data['starttime']=date('Y-m-d',$data['starttime']);
      // var_dump($data);die;
        $res  = Db::field('coursename')
              ->name('course')
              ->where('id ='.$data['coid'])
              ->find();
          // var_dump($res);die;
       return view('',[
         'data'=>$data,
         'res'=>$res
          ]);
    }

     //更新编辑的班期的信息
     public  function update(Request $request, $id)
    {

    
      $p = $request->post();
      // var_dump($p);die;
        $a =['coursename' => $p['coursename']];
       $tt = strtotime($p['starttime']); 
        // var_dump($tt);die;
        $data =[
        'classname' =>$p['classname'],
        'longtime' => $p['longtime'],
        'starttime'=>strtotime($p['starttime'])
       
        ];
         $b = Db::field('id')
                ->name('course')
                ->where(['coursename'=>$a['coursename']])
                ->find();
         $c = Db::field('coid')
                 ->name('classdate')
                 ->where(['coid'=>$b['id']])
                 ->find();
          // var_dump($b);die;
         $data['coid']=$c['coid'];
         // var_dump($data);die;
        $res = Db::name('classdate')
                    ->where(['id'=> $id])
                    ->update($data);
         // var_dump($res);die;
        if($res > 0){
           $this->success('编辑成功',url('admin/classdate/index'));
        }else{
       $this->error('编辑失败,请重试');
    } 
}
          //删除班期
     public function delete($id)
     {

        $res = Db::name('classdate')
                 ->delete($id);
        // var_dump($res);die;
        if($res >0){
            $this->success('删除成功',url('admin/course/index')); 
        }else{
            $this->error('删除失败,请重新删除');
        }
}
}